<html>
    <head>
        <script src="jquery-1.5.min.js"></script>
        <script src="jquery.textarea.js"></script>
        <script>
            var poller; 

            function Poller(idin, waittimein){
                this.id = idin;
                this.waittime = waittimein || 10;
                this.setTimeout = setTimeout(poll, waittimein);
            }

            poll = function (){
                $.ajax({
                    url: '/get',
                    data: {
                        id: poller.id 
                    },
                    success: function (raw_data){
                        data = JSON.parse(raw_data);
                        if (data.status !== 'wait'){
                            $('#loader').hide(); 
                            $('#answer').show(); 
                            $('#answer').html(data.output); 
                            $('#input').attr("disabled","");
                        } else {
                            poller.settimeout = setTimeout(poll, poller.waittime);
                        }
                    }                     
                });

            }

            function execute(input){
                /* console.log('exec', input); */
                $.ajax({
                    url: '/execute',
                    data: {
                        input: input
                    },
                    success: function (data){
                        /* console.log('entered with id', data); */
                        poller = new Poller(data);
                        $('#loader').show();
                        $('#input').attr("disabled","disabled");
                    }                     
                });
            }

            $(document).ready(function (){
                $('#loader').hide();
                $('#input').tabby();
                $('#input').keydown(function (e){
                    if (e.which == 13 && e.shiftKey ){
                        execute($('#input').val());
                        return false;
                    }
                });
                $('#compute_button').click(function (){
                    execute($('#input').val());
                });
            });
        </script>
    <title>Sage Aleph</title></head>
<body>
        <textarea cols=80 rows=5 id='input'>2 + 2</textarea>
        <br>
        <input type='button' id='compute_button' value='compute'>
        <br>
            <img id='loader' src='ajax-loader.gif'>
        <br>
        <pre id='answer'></pre>
</body></html>
